package com.example.facemanager.config;

import com.example.facemanager.service.AuthService;
import com.example.facemanager.vo.GroupInfoState;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @Author huchenying
 * @Date 2022/8/3 10:05
 * @Description
 */
@Slf4j
public class Memory {
    public static String access_token = "";
    public static Long ttl = 0L;

    public static List<GroupInfoState> groupInfoStates = new ArrayList<>();

    public static boolean isTimeout() {
        return ttl + 30 * 12 * 60 * 60 * 1000 < System.currentTimeMillis();
    }

    public static String getAccess_token() {
        if (isTimeout()) {
            log.info("rethinking token.");
            AuthService.getAuth();
        }
        return access_token;
    }

    public static List<GroupInfoState> getGroupInfoStates(){
        return groupInfoStates;
    }

    public static boolean deleteGroupInfoStates(GroupInfoState groupInfoState){
        return groupInfoStates.removeIf(next -> next.getGroupName().equals(groupInfoState.getGroupName()));
    }

    public static boolean addGroupInfoStates(GroupInfoState groupInfoState){
        return groupInfoStates.add(groupInfoState);
    }
}
